package com.rd.student.jike.class2.lihongming.Slolution3;

/**
 * @Auther:Heinrich
 * @Date:2023/7/28-07-28-11:26
 * @Description:com.rd.student.jike.class2.lihongming.exercise3
 * @version:1.0
 */
public class Question5 {
    public static void main(String[] args) {
        PrintCharNums Num = new PrintCharNums(1);
        PrintNums printNums = new PrintNums(Num);
        PrintChars printChars = new PrintChars(Num);
        new Thread(printNums).start();
        new Thread(printChars).start();
    }
}
class PrintChars implements Runnable{
    PrintCharNums Num ;

    public PrintChars(PrintCharNums num) {
        Num = num;
    }

    @Override
    public void run() {
        Num.printChar();
    }
}
class PrintNums implements Runnable{
    PrintCharNums Num ;

    public PrintNums(PrintCharNums num) {
        Num = num;
    }
    @Override
    public void run() {
        Num.printNum();
    }
}
class PrintCharNums{
    int Num;

    public PrintCharNums(int num) {
        Num = num;
    }
    public void printChar(){
        for (int i = 1; i <= 26; i++) {
            synchronized (this) {
                while (Num != 3) {
                    try {
                        this.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.print((char) (96 + i));
                Num = 1;
                this.notifyAll();
            }
        }
    }
    public void printNum(){
        for (int i = 1; i <= 52; i++) {
            synchronized (this) {
                while (Num != 1&&Num != 2) {
                    try {
                        this.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.print(i);
                Num++;
                this.notifyAll();
            }
        }
    }

}
